package com.monster.datasourcejob.typeconverter.impl;

import com.monster.datasourcejob.typeconverter.TypeConverter;
import com.monster.datasourcejob.typeconverter.fieldtype.HiveType;
import com.monster.datasourcejob.typeconverter.fieldtype.MysqlType;

import java.util.HashMap;
import java.util.Map;

/**
 * description:
 *
 * @author Yuanml on 2021/6/29 15:39
 */
public class Mysql2HiveTypeConverter implements TypeConverter {
    public static final Mysql2HiveTypeConverter INSTANCE = new Mysql2HiveTypeConverter();
    private final static Map<MysqlType, HiveType> TYPE_REPOSITORY = new HashMap<>();
    static {
        TYPE_REPOSITORY.put(MysqlType.DECIMAL, HiveType.DECIMAL);
        TYPE_REPOSITORY.put(MysqlType.TINYINT, HiveType.TINYINT);
        TYPE_REPOSITORY.put(MysqlType.BOOLEAN, HiveType.BOOLEAN);
        TYPE_REPOSITORY.put(MysqlType.SMALLINT, HiveType.SMALLINT);
        TYPE_REPOSITORY.put(MysqlType.INT, HiveType.INT);
        TYPE_REPOSITORY.put(MysqlType.FLOAT, HiveType.FLOAT);
        TYPE_REPOSITORY.put(MysqlType.DOUBLE, HiveType.DOUBLE);
        // TYPE_REPOSITORY.put(MysqlType.NULL, HiveType.NULL);
        TYPE_REPOSITORY.put(MysqlType.TIMESTAMP, HiveType.TIMESTAMP);
        TYPE_REPOSITORY.put(MysqlType.BIGINT, HiveType.BIGINT);
        // TYPE_REPOSITORY.put(MysqlType.MEDIUMINT, HiveType.MEDIUMINT);
        TYPE_REPOSITORY.put(MysqlType.DATE, HiveType.DATE);
        // TYPE_REPOSITORY.put(MysqlType.TIME, HiveType.TIME);
        TYPE_REPOSITORY.put(MysqlType.DATETIME, HiveType.STRING);
        // TYPE_REPOSITORY.put(MysqlType.YEAR, HiveType.YEAR);
        TYPE_REPOSITORY.put(MysqlType.VARCHAR, HiveType.STRING);
        // TYPE_REPOSITORY.put(MysqlType.VARBINARY, HiveType.VARBINARY);
        // TYPE_REPOSITORY.put(MysqlType.BIT, HiveType.BIT);
        // TYPE_REPOSITORY.put(MysqlType.JSON, HiveType.JSON);
        // TYPE_REPOSITORY.put(MysqlType.ENUM, HiveType.ENUM);
        // TYPE_REPOSITORY.put(MysqlType.SET, HiveType.SET);
        TYPE_REPOSITORY.put(MysqlType.TINYBLOB, HiveType.STRING);
        TYPE_REPOSITORY.put(MysqlType.TINYTEXT, HiveType.STRING);
        // TYPE_REPOSITORY.put(MysqlType.MEDIUMBLOB, HiveType.MEDIUMBLOB);
        // TYPE_REPOSITORY.put(MysqlType.MEDIUMTEXT, HiveType.MEDIUMTEXT);
        // TYPE_REPOSITORY.put(MysqlType.LONGBLOB, HiveType.LONGBLOB);
        // TYPE_REPOSITORY.put(MysqlType.LONGTEXT, HiveType.LONGTEXT);
        // TYPE_REPOSITORY.put(MysqlType.BLOB, HiveType.BLOB);
        TYPE_REPOSITORY.put(MysqlType.TEXT, HiveType.STRING);
        TYPE_REPOSITORY.put(MysqlType.CHAR, HiveType.STRING);
        TYPE_REPOSITORY.put(MysqlType.BINARY, HiveType.BINARY);
        // TYPE_REPOSITORY.put(MysqlType.GEOMETRY, HiveType.GEOMETRY);
        // TYPE_REPOSITORY.put(MysqlType.UNKNOWN, HiveType.UNKNOWN);
    }

    private Mysql2HiveTypeConverter() {

    }

    @Override
    public String convert(String type) {
        int indexOf = type.indexOf("(");
        if (indexOf != -1) {
            type = type.substring(0, indexOf);
        }
        MysqlType mysqlType = MysqlType.getByTypeName(type);
        HiveType hiveType = TYPE_REPOSITORY.get(mysqlType);
        if (hiveType == null) {
            throw new IllegalArgumentException(String.format("can't convert mysql type [%s] to hive type", type));
        }
        return hiveType.getTypeName();
    }
}
